/**
* 用户详情对话框（可用于添加和修改对话框）
*/
var ActivityBatchInfoDlg = {
    id: "courseTable",
    width: '650px',
    height: '850px',
    currentLayerIndex: -1,
    cache: "",
    InfoData: {},
    courseTableData:[],
    me:"",
    seItem: null,		//选中的条目
    selectData: null
};

/**
 * 初始化新增弹出框
 */
ActivityBatchInfoDlg.init = function () {
    this.cache = $("#activityBatchDialog").html();
    $("#activityBatchDialog").html('');
};

/**
 * 清除数据
 */
ActivityBatchInfoDlg.clearData = function () {
    this.InfoData = {};
    this.courseTableData = [];
};

/**
 * 关闭此对话框
 */
ActivityBatchInfoDlg.close = function () {
    layer.close(this.currentLayerIndex);
    ActivityBatchInfoDlg.clearData();
};

/**
 * 新增打开模态框调用事件
 */
ActivityBatchInfoDlg.renderAdd = function (index) {
    //初始化科目
    ActivityBatchInfoDlg.initClass();
    // 初始化校区
    ActivityBatchInfoDlg.initSchool();

    ActivityBatchInfoDlg.initTable();

/*    var selected = $('#' + this.id).bootstrapTable('getSelections');
    ActivityBatchInfoDlg.me.btInstance.on('check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table', function (e, rows) {
        ActivityBatchInfoDlg.seItem = $.map(ActivityBatchInfoDlg.me.btInstance.bootstrapTable('getSelections'), function (row) {

            var item =  sessionStorage.getItem("ActivityBatchInfoDlg.key");
            var itemArray = [];
            if (item != null && item != undefined){
                itemArray = item.split(',');
            }
            itemArray.push(row.id);
            var _select = [];// 去重
            var index = itemArray.indexOf(row.id);
            $.each(itemArray,function(i,id){
                if ($.inArray(id,_select) == -1){
                    _select[_select.length] = id;
                }
            });

            sessionStorage.setItem("ActivityBatchInfoDlg.key",itemArray.toString()) ;
            console.log(sessionStorage.getItem("ActivityBatchInfoDlg.key"));
            return row.id;
        });
    });*/
};
/**
 *  初始化科目
 */
ActivityBatchInfoDlg.initClass = function () {

    var ajax = new $ax(Feng.ctxPath + "/classType/allParam", function (data) {
        var _select = "<option value='' selected>--请选择科目--</option>";
        data = data.records ||[];
        if (data.length > 0){
            for (var i = 0;i < data.length;i++){
                _select += "<option value='{0}'>{1}</option>".format(data[i].id,data[i].classTypeName);
            }
            $("#txt_search_classType").html(_select);
        }
    }, function (data) {
        Feng.error("查询科目失败!" + data.responseJSON.message + "!");
    });
    ajax.setData({
        current:1,
        size:999
    });
    ajax.start();
}
/**
 *  初始化校区
 */
ActivityBatchInfoDlg.initSchool = function () {

    var ajax = new $ax(Feng.ctxPath + "/school/allParam", function (data) {
        var _select = "<option value='' selected>--请选择校区--</option>";
        data = data.records ||[];
        if (data.length > 0){
            for (var i = 0;i < data.length;i++){
                _select += "<option value='{0}'>{1}</option>".format(data[i].id,data[i].schoolName);
            }
            $("#txt_search_school").html(_select);
        }
    }, function (data) {
        Feng.error("选择校区失败!" + data.responseJSON.message + "!");
    });
    ajax.setData({
        current:1,
        size:999
    });
    ajax.start();
}

/**
 * 搜索
 */
ActivityBatchInfoDlg.clickSearch_conurse = function(){
    var queryData = {};
    var val_search_courseClass = $("#txt_search_courseClass").val();
    if (val_search_courseClass !== "" || val_search_courseClass !== null) {
        queryData['courseClass'] = val_search_courseClass;
    }

    var val_search_courseGrade = $("#txt_search_courseGrade").val();
    if (val_search_courseGrade !== "" || val_search_courseGrade !== null) {
        queryData['courseGrade'] =  val_search_courseGrade;
    }

    var val_search_classTypeId = $("#txt_search_classType").val();
    if (val_search_classTypeId !== "" || val_search_classTypeId !== null) {
        queryData['classTypeId'] =  val_search_classTypeId;
    }

    var val_search_campus = $("#txt_search_school").val();
    if (val_search_campus !== "" || val_search_campus !== null) {
        queryData['campus'] =  val_search_campus;
    }
    queryData['hasBinding'] = 'notBind';
    ActivityBatchInfoDlg.me.refresh({query: queryData});
};



/**
 * 初始化整个表格
 */
ActivityBatchInfoDlg.initTable = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(ActivityBatchInfoDlg.id,Feng.ctxPath+"/course/allParam",defaultColunms);
    bstable.setData({
        hasBinding: 'notBind',
        current: 1,
        size: 999
    });
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
ActivityBatchInfoDlg.initColumn = function(){
    var columns = [
        {field: 'checkStatus',checkbox:true},
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '课程名称',field: 'courseName',align: 'center',valign: 'middle'},
        {title: '大类',field: 'courseClass',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if (value == '1'){
                    return "课外辅导";
                }else if(value == '2'){
                    return "研学旅行";
                }else if (value == '3'){
                    return "冬夏令营";
                }else if (value == '4'){
                    return "线上辅导";
                }
            }},
        {title: '大类',field: 'courseGrade',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if (value == '1'){
                    return "一年级";
                }else if(value == '2'){
                    return "二年级";
                }else if (value == '3'){
                    return "三年级";
                }else if (value == '4'){
                    return "四年级";
                }else if (value == '5'){
                    return "五年级";
                }else if (value == '6'){
                    return "六年级";
                }else if (value == '7'){
                    return "初一";
                }else if (value == '8'){
                    return "初二";
                }else if (value == '9'){
                    return "初三";
                }
            }},
        {title: '课程季',field: 'courseSeason',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                if (value == 'spring'){
                     return "春季";
                }else if(value == 'summer'){
                    return "夏季";
                }else if (value == 'autumn'){
                    return "秋季";
                }else if (value == 'winter'){
                    return "冬季";
                }
            }},
        {title: '课程单价',field: 'coursePrice',align: 'center',valign: 'middle'},
        {title: '总课次',field: 'totalNumber',align: 'center',valign: 'middle'}];
    return columns;
};


/**
 * 活动批量绑定课程
 */
ActivityBatchInfoDlg.batchBindSubmit = function (activityId) {
    //复选框勾选
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length <= 0) {
        Feng.error("请选择课程数>0");
        return;
    } else {
        var _select = [];// 去重
        $.each(selected,function(i,course){
            if ($.inArray(course.id, _select) === -1) {
                _select[_select.length] = course.id;
            }
        });
        ActivityBatchInfoDlg.seItem = _select.toString();
        ActivityBatchInfoDlg.selectData = selected;
    }
    //新增时判断activityId是否为空
    if(activityId !== ""){
        //保存请求
        var option = {
            activityId: activityId,
            courseIds: ActivityBatchInfoDlg.seItem
        };
        var ajax = new $ax(Feng.ctxPath + "/activity/bindingCourse", function (data) {
            if(data.success){
                Feng.success("操作成功");
                ActivityBatchInfoDlg.clearData();
                ActivityBatchInfoDlg.me.refresh();
            } else if (data.code === 2) {
                Feng.error(data.msg);
            } else {
                Feng.error("操作失败");
            }
        }, function (data) {
            Feng.error("绑定失败!" + data.responseJSON.message + "!");
        });
        ajax.setData(option);
        ajax.start();
    }

};






